package com.command
{
	import com.model.CommonModel;
	import com.model.ModelLocator;
	import com.services.IService;
	import com.services.ServiceLocator;
	import com.vo.ICPDocumentsVO;
	import com.vo.ValueObject;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	import mx.utils.ObjectUtil;

	public class ICPDocumentsFetchCommand implements IService, IResponder
	{
		[Bindable]private var commonModel:CommonModel = ModelLocator.getInstance().commonModel;		
		public function execute(valueObject:ValueObject):void
		{			
			var service:RemoteObject = ServiceLocator.getInstance().getRemoteObject("ICPDocumentsService");	
			if(ICPDocumentsVO(valueObject).objectID)
			{		
				var token:AsyncToken= service.selectForObject(ICPDocumentsVO(valueObject).objectID);
				token.addResponder(this);
			}
			else
			{
				var token1:AsyncToken = service.select(ICPDocumentsVO(valueObject).id);
				token1.addResponder(this);
			}
		}
		
		public function result(data:Object):void
		{
			commonModel.icpDocuments = ArrayCollection(ResultEvent(data).result);	
		}
		
		public function fault(info:Object):void
		{
			Alert.show("Error \n"+ObjectUtil.toString(FaultEvent(info).fault));
		}
		
	}
}